<?php
/**
 * Created by PhpStorm.
 * User: wxh
 * Date: 15-9-11
 * Time: 上午10:25
 */
error_reporting(E_ERROR | E_PARSE);

if(!$_SESSION['sid']) exit('{"sta":"fail","fail_msg":"登录超时"}');
require_once("/var/www/html/new/conf/le.je");
$le=new go_sql();
$le->connect();
$dname = array();
$status = array(
    '<em style="color: #303d46">未使用</em>',
    '<em style="color: #3dba31">已使用</em>',
    '<em style="color: #92a0ab">已过期</em>',
    '<em style="color: #92a0ab">已取消</em>',
    '<em style="color: #92a0ab">凭证码被替代</em>',
    '<em style="color: #92a0ab">已撤改</em>',
    '<em style="color: #92a0ab">已撤销</em>',
);
$status_excel = array(
    '未使用',
    '已使用',
    '已过期',
    '已取消',
    '已使用',
    '已使用',
);
$paystatus = array(
    '现场支付',
    '已支付',
    '未支付'
);
$ordermode = array(
    0=>'',
    1=>'<br/>散客预订',
    9=>'<br/>现场购票',
);
$pmode = array(
    '账户余额',
    '支付宝',
    '授信支付',
    '自供自销',
    '现场支付',
    '微信支付',
    '银联支付',
);
$p_type = array(
    'A'=>'景区',
    'B'=>'线路',
    'C'=>'酒店',
    'F'=>'套票',
    'G'=>'餐饮',
    'H'=>'演出',
);

$result = handle_param($_POST);
print_r($result);
echo json_encode($result);




echo '<pre>';
print_r($_POST);
echo '</pre>';
exit;

/*
// * 传入的数据:
// * 时间类型 time_type:1下单 2预计 3完成  具体时间 开始btime 结束etime
// * 第二行的选择框类型 select_type: 1订单号 2产品名称 3供应商/分销商 4手机号/游客姓名 5远程订单号
// * 输入框内的参数名 select_text
// * 支付方式 pmode 依次排列 从0开始
// * 订单状态 order_status 0未使用|1已使用|2已过期|3被取消
// * 支付与否用另一个数组 pay_status
// * 当前页  current_page
// * 每页数量 page_size
// * 当点击导出数据时 多传一个参数 excel=1
// * */
function handle_param($post_param){
    extract($post_param);
    $btime = $btime ? $btime : date('Y-m-d').' 00:00:00';
    $etime = $etime ? $etime : date('Y-m-d').' 23:59:59';
    $etime = str_replace('24:00','23:59',$etime);
    $btime1=$etime1=$btime2=$etime2=$btime3=$etime3=$ordernum=$ltitle=$amid=$otel=$oname=$remotenum=$pmode_str=$pay_status=$order_status_str='';
    $select_text = mysql_real_escape_string(trim($select_text));
    switch ($time_type) {
        case '1':
        default:
            $btime1 = $btime;
            $etime1 = $etime;
            break;
        case '2':
            $btime2 = $btime;
            $etime2 = $etime;
            break;
        case '3':
            $btime3 = $btime;
            $etime3 = $etime;
            break;
    }
    //1订单号 2产品名称 3远程订单号 4手机号/取票人 5供应商/分销商
    switch ($select_type) {
        case '1':
        default:
            $ordernum = $select_text;
            break;
        case '2':
            $ltitle = $select_text;
            break;
        case '5':
            if($select_text==$_SESSION['dname']) break;//真是蛋疼
            $select_text = mysql_real_escape_string($select_text);
            $sql = "select id from pft_member where instr(dname,'$select_text') limit 1";
            $GLOBALS['le']->query($sql);
            $GLOBALS['le']->fetch_assoc();
            $amid = $GLOBALS['le']->f('id');
            break;
        case '4':
            if(strlen($select_text)==11 && $select_text>10000000000)
                $otel = $select_text;
            else
                $oname = $select_text;
            break;
        case '3':
            $remotenum = $select_text;
            break;
    }
    //$order_status 和 $pay_status 都可以竖线隔开  不过页面上只能选择支付或者未支付
    //$pay_status 当勾了时 只查未支付的 不勾 就查全部

    $pmode = $pmode ? $pmode+0:'';
//    if($pmode==4) $pmode='';//自供自销的支付方式在后面筛选
    if(is_array($order_status))
        foreach($order_status as $v){
            if($v==0)
                $pay_status=2;
            else
                $order_status_str .= ($v).'|';
        }
    else
        $order_status_str = $order_status;
    $order_status_str = trim($order_status_str,'|');
    //排序 默认按下单时间降序
    $orderby = $orderby ? $orderby : 1;
//    $orderby = 2;
    if($excel==1){
        $offset = 0;
        $page_size = 99999;
    }else{
        $current_page = ($current_page+0)<2 ? 0 :$current_page-1;
        $offset = $current_page * $page_size;
        $page_size = ($page_size+0)==0 ? 15 :$page_size;
    }
    $param = array(
        'amid'=>$amid,
        'ltitle'=>$ltitle,
        'btime1'=>$btime1,
        'etime1'=>$etime1,
        'btime2'=>$btime2,
        'etime2'=>$etime2,
        'btime3'=>$btime3,
        'etime3'=>$etime3,
        'ordernum'=>$ordernum,
        'oname'=>$oname,
        'otel'=>$otel,
        'order_status'=>$order_status_str,
        'pay_status'=>$pay_status,
        'pmode'=>$pmode,
        'offset'=>$offset,
        'orderby'=>$orderby,
        'page_size'=>$page_size,
        'remotenum'=>$remotenum,
        'excel'=>$excel,
    );
    return handl_order($param);
}
//
////1507516 联票订单号
function handl_order($param){
    $total = array();

    $param['salerid'] = '';
    if($_SESSION['dtype'] == 2){
        $param['salerid']=$_SESSION['sid'];
    }else{
        $self=$_SESSION['sid'];
    }

    //查卖出的订单
    $param['mid']=$param['amid'];
    $param['aid']=$self;
    $param['c'] = 0;
    $x1 = simplexml_load_string(order_search($param));

    $param['c'] = 1;
    $c1 = order_search($param);

    $x2 = new stdClass();
    $x2->Rec= array();
    if($_POST['pmode']!=4){//查买到的订单
        $param['aid']=$param['amid'];
        $param['mid']=$self;
        //景区的话 不可能买别人的产品
        if($_SESSION['dtype'] == 2){
            $param['aid'] = '';
        }

        $param['c'] = 0;
        $x2 = simplexml_load_string(order_search($param));
    }

    $param['c'] = 1;
    $c2 = order_search($param);
    $total['all_torder'] = max($c1,$c2);
    $total['all_page']   = (int)($total['all_torder']/$param['page_size']);
    $total['all_page'] = ($total['all_torder']%10)!=0 ? $total['all_page']+1 : $total['all_page'];


    $lian= array();
    foreach($x1->Rec as $v1){
        if((int)$v1->UUconcat_id!=0){//联票只统计一次主票 之后就不统计了
            if(in_array((int)$v1->UUconcat_id,$lian)) continue;
            $lian[] = (int)$v1->UUconcat_id;
        }
        //自供自销的筛选
        if($_POST['pmode']==4 && (int)$v1->UUpmode!=3){
            continue;
        }
        $row = row($v1,1,array(),$param['excel']);
        $GLOBALS['tid_mark'][$row['ordernum'][0]] = 1;//买到和卖出同时有订单的话 防止重复写入数据
        ++$total['torder'];
        //$total['torder'] += count($row['tnum']);
        $total['tnum']+=array_sum($row['tnum']);
        $total['sell_money']+=$row['sell_money'];
        $list[$row['ordernum'][0]] = $row;
    }
    $lian = array();
    foreach($x2->Rec as $v2){
        if((int)$v2->UUconcat_id!=0){//联票只统计一次主票 之后就不统计了
            if(in_array((int)$v2->UUconcat_id,$lian)) continue;
            $lian[] = (int)$v2->UUconcat_id;
        }
        $row = $list[(int)$v2->UUordernum];
        $row = row($v2,2,$row,$param['excel']);
        if(!$list[(int)$v2->UUordernum]){//如果在卖出的订单里没统计过这个订单的话 加上订单数和门票数
            $total['tnum']+=array_sum($row['tnum']);
        }
        $total['buy_money']+=$row['buy_money'];
        $list[$row['ordernum'][0]] = $row;
    }
    $total['torder'] = $list ? count($list) : 0 ;
    $total['tnum'] = $total['tnum'] ? $total['tnum'] : 0 ;
    $total['buy_money'] = $total['buy_money'] ? $total['buy_money'] : 0 ;
    $total['sell_money'] = $total['sell_money'] ? $total['sell_money'] : 0 ;
    //查所有涉及到的用户名称
    $GLOBALS['dname'] = implode(',',$GLOBALS['dname']);
    if($GLOBALS['dname'] != ''){
        $sql = "select id,dname from pft_member where id in ({$GLOBALS['dname']})";
        $GLOBALS['le']->query($sql);
        $GLOBALS['dname'] = array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $GLOBALS['dname'][$row['id']] = $row['dname'];
        }
        foreach($list as $k => $v){
            $list[$k]['buy_id'] = $GLOBALS['dname'][$v['buy_id']];
            $list[$k]['sell_id'] = $GLOBALS['dname'][$v['sell_id']];
        }
    }
    if($param['orderby']==1){//按下单时间排序 直接对订单号来处理就可以了
        krsort($list);
        //krsort后 键没有改变 只是单纯的改变了下各个值的顺序 所以还需要重新foreach一次来真正的排序
        foreach($list as $v){
            $new_list[] = $v;
        }
    }elseif($param['orderby']==3){//按完成时间排序
        foreach($list as $v){
            $sort[] = $v['dtime'];
        }
        array_multisort($sort,SORT_DESC,$list);
        $new_list = $list;
    }

    if($param['excel']==1){
        excel($new_list);
    }
    return array(
        'sta'=>'suc',
        'list'=>$new_list,
        'total'=>$total
    );
}
/*
 * 传回的数据:
 * 订单的全部放在list里面
 * 订单号   ordernum
 * 下单时间 ordertime
 * 有效时间 begin_to_end
 * 产品名称 ltitle 产品ID lid (隐藏)
 * 票类名称 ttitle 票类ID tid (隐藏)  数量tnum 买入卖出价 tprice
 * 备注:上一行的参数都是数组 需要遍历输出 tprice直接就是70/80的样子
 * 总金额 total_buy_money total_sell_money
 * 取票人 ordername  电话  ordertel
 * 支付方式分两行 pmode_1 pmode_2
 * 供应商: a_name 分销商 m_name
 * 订单状态:order_status 我会把字用span包好 并且设定好颜色 直接html()进去就可以了
 * 操作: 取消 order_cancel 修改 order_modify 重发 sms_resend  验证 order_check  支付 order_pay
 * 页面中部的数据 全部放在totol里面:订单数 torder 票数 tnum 买入金额 buy_money 卖出金额 sell_money
 * JSON大概是这样的{
 *      sta:'suc',
 *      list:[],
 *      total:{
 *          torder:123,
 *          tnum: 234,
 *          buy_money:1000,
 *          sell_money:1000,
 *      }
 * }
 * */


/*
 * $v 订单数组
 * $sob sell or buy  1是登录用户是出售者的情况 2是登录用户是购买者的情况
 * $row  因为订单要以购买者和出售者嵌套循环 所有如果之前$row里有值了 就再传到这个函数里继续添加值
 * $repeat 为false时 不再取景区名 取票人等一些相同的信息
 * */
function row($v,$sob,$row=array(),$excel=false){
    if(!$row['ordernum']){
        $row['ltitle'] = (string)$v->UUltitle;//景区标题
        $row['lid'] = (int)$v->UUlid;//景区ID
        $row['status'] = $GLOBALS['status'][(int)$v->UUstatus];//订单使用状态
        $row['begin_to_end'] = (string)$v->UUendtime;//有效期 只限时到期时间
        $row['ordername'] = (string)$v->UUordername;//游客姓名
        $row['ordertel'] = (string)$v->UUordertel;//游客电话
        $row['ordertime'] = substr((string)$v->UUordertime,0,16);//下单时间 截取至分
        $row['ordermode'] = $GLOBALS['ordermode'][(int)$v->UUordermode];
        $row['dtime'] = substr((string)$v->UUdtime,0,4)=='0000'?'未游玩':(string)$v->UUdtime;//使用(游玩)时间
        if($excel){
            $row['paystatus']= $GLOBALS['paystatus'][(int)$v->UUpaystatus];//支付状态
            $row['code'] = (string)$v->UUcode;//验证码
            $row['dtime'] = (string)$v->UUdtime;//使用(游玩)时间
            $row['memo'] = (int)$v->UUmemo;//备注
            $row['remotenum'] = (int)$v->UUremotenum;//远端订单号
            $row['status'] = $GLOBALS['status_excel'][(int)$v->UUstatus];//订单使用状态
            $row['p_type'] = $GLOBALS['p_type'][(string)$v->UUp_type];//产品类型
            if((string)$v->UUpersonid!='0'){
                $tourists = array();
                $sql = "select idcard,tourist from uu_order_tourist_info where orderid=".(int)$v->UUordernum;
                $GLOBALS['le']->query($sql);
                while($ro=$GLOBALS['le']->fetch_assoc())
                    $tourists[$ro['idcard']] = $ro['tourist'];
            }
            if(count($tourists)){
                foreach($tourists as $idcard => $tourist){
                    $row['personid'] .= $idcard.':'.$tourist.'||';
                }
                $row['personid'] = rtrim($row['personid'],'||');
            }else
                $row['personid'] = (string)$v->UUpersonid!='0'?
                    (string)$v->UUpersonid.':'.$row['ordername']:'用户没有输入身份证号码';
        }
    }

    if((int)$v->UUconcat_id!=0){
        $param['ordernum'] = (int)$v->UUconcat_id;
        $param['concat'] = 1;
        $param['offset'] = 0;
        $param['page_size'] = 15;
        if($sob == 1) $param['aid'] = $_SESSION['sid'];
        else $param['mid'] = $_SESSION['sid'];
        $console = simplexml_load_string(order_search($param));
        $row['ordernum'][] = (int)$v->UUconcat_id;
    }else{
        $console = new stdClass();
        $console->Rec= $v;
        $row['ordernum'][] = (int)$v->UUordernum;
    }

    foreach($console as $v){
        if((int)$v->UUconcat_id != (int)$v->UUordernum && (int)$v->UUconcat_id != 0)
            $row['ordernum'][] = (int)$v->UUordernum;
        //判断订单取消\修改等权限 是否处理过一次 否则初始化权限变量 全部为没有
        if(!$row['authMark']){
            $row['orderAlipay']=2;
            $row['orderCancel']=2;
            $row['orderAlter'] =2;
            $row['orderResend']=2;
            $row['orderCheck']=2;
        }
//        var_dump($GLOBALS['tid_mark'][$row['ordernum'][0]]);
        if( !$GLOBALS['tid_mark'][$row['ordernum'][0]]){
            $row['data_ticket'] .= '【'.(string)$v->UUttitle.'】|'.(string)$v->UUtnum.'|'.(string)$v->UUtid.'&';//修改订单时需要用到的值
            $row['tid'][] = (int)$v->UUtid;
            $row['ttitle'][] = (string)$v->UUttitle;
            $row['tnum'][] = (int)$v->UUtnum;
        }


        //整合分销链
        if((string)$v->UUaids!='0'){
            $UUaids = (string)$v->UUaids.','.(string)$v->UUmid;
        }else if((int)$v->UUaid!==(int)$v->UUmid){
            $UUaids = (string)$v->UUaid.','.(string)$v->UUmid;
        }else{
            $UUaids = (string)$v->UUaid;
        }

        //整合分销价格链
        if((string)$v->UUaids_price != '0'){
            $UUaids_price = (string)$v->UUaids_price.','.(string)$v->UUtprice;
        }else{
            $UUaids_price = (string)$v->UUtprice;
        }
        $UUaids_price = explode(',', $UUaids_price);
        $UUaids = explode(',', $UUaids);
        $key = array_search($_SESSION['sid'],$UUaids);
        $aid_money = unserialize((string)$v->UUaids_money);
        //处理价钱 转分销上下级分别是谁
        if($sob == 1){//卖
            $row['sell_id'] = (int)$v->UUbuyid;//卖给谁
            $GLOBALS['dname'][$row['sell_id']]= $row['sell_id'];
            $sell_price = $UUaids_price[$key] ? $UUaids_price[$key] : 0 ;
            $row['sell_price'][] = $sell_price;
            $row['sell_money'] += $sell_price * (int)$v->UUtnum;
            if((int)$v->UUaid==$_SESSION['sid']){//是最后级供应商 直接取pmode
                $row['sell_pmode']=$GLOBALS['pmode'][(int)$v->UUpmode];
            }elseif($UUaids[0]==$_SESSION['sid']){//最初级供应商
                $row['sell_pmode'] = $aid_money[0][1]==1?
                    $GLOBALS['pmode'][0]:$GLOBALS['pmode'][2];
            }else{
                foreach($aid_money as $k => $val){
                    if($val[0]==$_SESSION['sid']){
                        $row['sell_pmode'] = $aid_money[$k+1][1]==1?
                            $GLOBALS['pmode'][0]:$GLOBALS['pmode'][2];
                        break;
                    }
                }
            }
        }elseif($sob == 2){//买
            $row['buy_id'] = (int)$v->UUsellid;//向谁买
            $GLOBALS['dname'][$row['buy_id']]= $row['buy_id'];
            if(count($UUaids)==1) //自供自销
                $buy_price = $UUaids_price[0];
            else
                $buy_price = $UUaids_price[$key-1] ? $UUaids_price[$key-1] : 0;
            $row['buy_price'][] = $buy_price;
            $row['buy_money'] += $buy_price * (int)$v->UUtnum;
            if((int)$v->UUmid==$_SESSION['sid']){//是最后的购买者 直接取pmode
                $row['buy_pmode']= $GLOBALS['pmode'][(int)$v->UUpmode];
            }else{
                foreach($aid_money as $k => $val){
                    if($val[0]==$_SESSION['sid']){
                        $row['buy_pmode'] = $aid_money[$k][1]==1?
                            $GLOBALS['pmode'][0]:$GLOBALS['pmode'][2];
                        break;
                    }
                }
            }
        }
        $row = format($v,$row);
    }


    if( $row['orderAlter'] == 1){
        $row['data_ticket'] = rtrim($row['data_ticket'],'&');
    }

    return $row;
}

//判断订单是否有取消重发之类的权限
function format($v,$row){

    $row['authMark']=1;

    $paystatus = (int)$v->UUpaystatus;
    $status = (int)$v->UUstatus;
    $buyid = (int)$v->UUbuyid;
    $sellid = (int)$v->UUsellid;
    $mid = (int)$v->UUmid;
    $pmode = (int)$v->UUpmode;
    $contacttel = (int)$v->UUcontacttel;
    $aid = (int)$v->UUaid;
    // 购买人支付未支付订单（支付宝订单）
    if($pmode==1 && $paystatus!=1 && $status==0 && $buyid==$_SESSION['sid']){
        $row['orderAlipay']=1;
    }
    // 散客未支付可取消
    if($contacttel == $_SESSION['account'] && $status==0) $row['orderCancel']=1;
    // 散客支付完有修改权限
    if($contacttel == $_SESSION['account'] && $paystatus==1 && $status==0){
        $row['orderAlter']=1;
    }
    if($paystatus==1){
        $fit = (int)$v->UUordermode;//其他下单方式 会员卡 鼓浪屿散客
        $fit = !(!$fit || $fit == 9);
        if($status==0 && $mid==$_SESSION['sid'] && !$fit){
            $row['orderAlter']=1;

        }

        //已过期的只有管理员可以取消
        if($status==2 && $_SESSION['sid'] == 1 && !$fit){
            $row['orderCancel']=1;
        }

        // 已过期最下级卖家可以取消
//        if($status==2 && $aid==$_SESSION['sid'] && !$fit){
//            $row['orderCancel']=1;
//        }

    }
    // 平台订单可以取消
    if((string)$row['mainOrder']->UUordername=='淘宝码商' && $paystatus==1){
        $fit = in_array((int)$v->UUordermode,array(1,2));
        if($status==0 && $buyid==$_SESSION['sid'] && !$fit){
            $row['orderCancel']=1;
        }
    }

    //现场支付（包含自销）是购买者 未使用 给取消权限
    if(($pmode==4 || $pmode==3) && $status==0 && $paystatus==1 && $buyid==$_SESSION['sid'] && !$fit){
        $row['orderCancel']=1;
    }
    if($sellid == $_SESSION['sid'] && $status== 0)
        $row['orderResend']=1;

    if( ($pmode==4 || $pmode==3) && $mid==$_SESSION['sid'] && $status==0)
        $row['orderAlter'] =1;
    //订单修改的权限 订单是否至少有一个订单是大于1的
    if($row['orderAlter'] == 1){
        foreach($row['tnum'] as $k => $val){
            if($val>1) break;//只要有一个订单数量是大于1 就能修改
            if(!$row['tnum'][$k+1])//遍历到最后一个时 还是没有大于1的订单 就取消修改权限
                $row['orderAlter'] = 2;
            continue;
        }
    }

    //验证的权限
    $pay = (int)$v->UUpaystatus=='0'?true : false;//0是现场支付

    if((string)$v->UUaids!='0'){//取最初的出售者
        $aid = explode(',',(string)$v->UUaids);
        $aid = $aid[0];
    }else
        $aid = (int)$v->UUaid;

    if((string)$v->UUp_type!='F' && //套票不显示
        (
            ((int)$v->UUstatus==0 && (int)$v->UUpaystatus==1) ||//已支付未使用
            ($pay==true && (int)$v->UUstatus==0) //现场支付未使用
        )&&
        $_SESSION['sid']!=1 &&//管理员不显示
        $aid==$_SESSION['sid']
    ){
        $row['orderCheck']=1;
        $row['code'] = (string)$v->UUcode;
    }
    //套票的子票不显示 修改 取消 重发
    if((int)$v->UUifpack == 2){
        $row['orderCancel']=2;
        $row['orderAlter'] =2;
        $row['orderResend']=2;
    }

    return $row;
}


function order_search($param){
    $s = new SoapClient(null,array(
            "location" => "http://localhost/open/openService/pft_insideMX.php",
            "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
    );
    extract($param);
//    echo '(
//    "'.$salerid.'","'.$mid.'", "", "", "'.$ltitle.'", "", "'.$btime1.'", "'.$etime1.'", "'.$btime2.'", "'.$etime2.'",
//        "'.$btime3.'", "'.$etime3.'", "'.$ordernum.'", "'.$oname.'", "'.$otel.'", "'.$order_status.'", "'.$pay_status.'", "", "'.$orderby.'", "1",
//        "'.$offset.'", "'.$page_size.'", "'.$c.'", "0","'.$pmode.'","0","'.$remotenum.'","0","","",
//        "'.$aid.'","", "","'.$concat.'","","0","",""
//    )';
//    exit;
    return $s->Order_Globle_Search(
        "$salerid","$mid", "", "", "$ltitle", "", "$btime1", "$etime1", "$btime2", "$etime2",
        "$btime3", "$etime3", "$ordernum", "$oname", "$otel", "$order_status", "$pay_status", "", "", "1",
        "$offset", "$page_size", "$c", "0","$pmode","0","$remotenum","0","","",
        "$aid","$concat", "","","","0","",""
    );

}


function excel($r){
    include("/var/www/html/new/d/class/SimpleExcel.class.php");

    $filename=date('YmdHis').'订单列表';

    $Excel[0]['UUordernum']='订单编号';
    $Excel[0]['UUordertime']='下单时间';
    $Excel[0]['UUbegintime']='有效期';
    $Excel[0]['UUdtime']='完成时间';
    $Excel[0]['UUpaystatus']='支付状态';
    $Excel[0]['UUseller']='下级分销商';
    $Excel[0]['UUbuyer']='上级供应商';
    $Excel[0]['UUordername']='取票人';
    $Excel[0]['UUpersonid'] = '身份证';
    $Excel[0]['UUordertel']='手机';
    $Excel[0]['UUp_type']='类别';
    $Excel[0]['UUltitle']='产品名称';
    $Excel[0]['UUsell_price']='出售单价(元)';
    $Excel[0]['UUbuy_price']='购买单价(元)';
    $Excel[0]['UUtnum']='门票数量';
    $Excel[0]['UUsell_pmode']='支付方式(卖出)';
    $Excel[0]['UUbuy_pmode']='支付方式(买入)';
    $Excel[0]['UUstatus']='订单状态';
    $Excel[0]['sell_money']='出售总金额(元)';
    $Excel[0]['buy_money']='购买总金额(元)';
    $Excel[0]['UUcode']='凭证码';
    $Excel[0]['UUmemo']='备注';
    $Excel[0]['UUremotenum']='远端订单号（防止科学记数法，订单前面加有 No: 字样）';

    $n=1;

    foreach($r as $row){
        foreach($row['tnum'] as $k => $v){
            $Excel[$n]['UUordernum']=$row['ordernum'][$k];
            $Excel[$n]['UUordertime']=$row['ordertime'];
            $Excel[$n]['UUbegintime']=$row['begin_to_end'];
            $Excel[$n]['UUdtime']=$row['dtime'];//完成时间
            $Excel[$n]['UUpaystatus']=$row['paystatus'];
            $Excel[$n]['UUseller']=$row['sell_id'];
            $Excel[$n]['UUbuyer']=$row['buy_id'];
            $Excel[$n]['UUordername']=$row['ordername'];
            $Excel[$n]['UUpersonid'] = $row['personid'];
            $Excel[$n]['UUordertel']=$row['ordertel'];
            $Excel[$n]['UUp_type']=$row['p_type'];
            $Excel[$n]['UUltitle']=$row['ltitle'];
            $Excel[$n]['UUsell_price']=$row['sell_price'][$k]/100;
            $Excel[$n]['UUbuy_price']=$row['buy_price'][$k]/100;
            $Excel[$n]['UUtnum']=$row['tnum'][$k];
            $Excel[$n]['UUsell_pmode']=$row['sell_pmode'];
            $Excel[$n]['UUbuy_pmode']=$row['buy_pmode'];
            $Excel[$n]['UUstatus']=$row['status'];
            $Excel[$n]['sell_money']=$Excel[$n]['UUsell_price']*$row['tnum'][$k];
            $Excel[$n]['buy_money']=$Excel[$n]['buy_price']*$row['tnum'][$k];
            $Excel[$n]['UUcode']=$row['code'];
            $Excel[$n]['UUmemo']=$row['memo'];
            $Excel[$n]['UUremotenum']= 'No:'.$row['remotenum'];
            ++$n;
        }

    }
}
//    exit;//    echo '<pre>';
//    print_r($Excel);
//    echo '</pre>';
//    exit;
    $xls = new SimpleExcel('UTF-8', true,'订单列表');
    $xls->addArray($Excel);
    $xls->generateXML($filename);

